package com.xzj.getway.controller;

import com.xzj.admin.base.domain.gateway.XzjRouteDefinition;
import com.xzj.common.core.domain.vo.R;
import com.xzj.getway.service.DynamicRouteServiceImpl;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: xzj-admin
 * @ClassName: RouteSettingController
 * @description: 网关API接口
 * @author: XiaZunJun
 * @create: 2024-10-11 15:15
 */
@RestController
public class RouteSettingController {

    private static final Logger log = LoggerFactory.getLogger(RouteSettingController.class);
    @Resource
    private DynamicRouteServiceImpl dynamicRouteService;

    @PostMapping("/refreshBatch")
    public R refreshBatch(@RequestBody List<XzjRouteDefinition> myRouteDefinitions) {
        try{
            dynamicRouteService.refresh(myRouteDefinitions);
            return R.success().putMsg("success");
        }catch (Exception e){
            return R.fail().putMsg("error");
        }
    }

    @PostMapping("/refreshSign")
    public R refreshSign(@RequestBody XzjRouteDefinition myRouteDefinition) {
        try{
            List<XzjRouteDefinition> myRouteDefinitions = new ArrayList<>();
            myRouteDefinitions.add(myRouteDefinition);
            dynamicRouteService.refresh(myRouteDefinitions);
            return R.success().putMsg("success");
        }catch (Exception e){
            return R.fail().putMsg("error");
        }
    }

    @GetMapping("/refreshAll")
    public R refreshAll(){
        try {
            dynamicRouteService.refreshAll();
            return R.success().putMsg("success");
        } catch (Exception e) {
            log.info("同步异常",e);
            return R.fail().putMsg("error:"+e.getMessage());
        }
    }


    @GetMapping("/delete/{id}")
    public R delete(@PathVariable(name = "id") String id) {
        try{
            dynamicRouteService.delete(id);
            return R.success().putMsg("success");
        }catch (Exception e){
            return R.fail().putMsg("error");
        }
    }
}
